import { createStore, useStore as baseUseStore, Store } from 'vuex';
import { InjectionKey } from 'vue';
export interface State {
    userName: string;
}

// 定义 injection key
export const key: InjectionKey<Store<State>> = Symbol('');

// 创建store实例
export const store = createStore<State>({
    state() {
        return {
            userName: localStorage.getItem('username') || '',
        };
    },
    mutations: {
        setUserName(state, data) {
            state.userName = data;
        },
    },
    actions: {
        setUserName({ commit }, data) {
            commit('setUserName', data);
        },
    },
});

// 定义自己的 `useStore` 组合式函数
export function useStore() {
    return baseUseStore(key);
}
